<!doctype html>
<html lang="zh-CN">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="referrer" content="no-referrer-when-downgrade">
    

    <title>gorm的查询 | 早起的年轻人</title>
    <meta property="og:title" content="gorm的查询 - 早起的年轻人">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2021-09-06T23:10:11&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2021-09-06T23:10:11&#43;08:00'>
        
    <meta name="Keywords" content="Flutter，golang,go语言,go语言笔记,飞雪无情,java,android,博客,项目管理,python,软件架构,公众号,小程序">
    <meta name="description" content="gorm的查询">
        
    <meta name="author" content="luckly">
    <meta property="og:url" content="https://luckly.work/post/go_gorm/CRUD%E7%9A%84%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C/">
    <link rel="shortcut icon" href='/favicon.ico'  type="image/x-icon">

    <link rel="stylesheet" href='/css/normalize.css'>
    <link rel="stylesheet" href='/css/style.css'>
    <script type="text/javascript" src="//cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    
    
    
        <link href="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" rel="stylesheet">
    
    
    
    
        <link rel="stylesheet" href='/css/douban.css'>
    
        <link rel="stylesheet" href='/css/other.css'>
    
</head>

<link rel="stylesheet"
      href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://luckly.work/">
                        早起的年轻人
                    </a>
                
                <p class="description">专注于Flutter、Android、Java、Go语言(golang)、移动互联网、项目管理、软件架构</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="https://luckly.work/">首页</a>
                    
                    <a  href="https://luckly.work/categories/" title="分类">分类</a>
                    
                    <a  href="https://luckly.work/tags/" title="标签">标签</a>
                    
                    <a  href="https://luckly.work/archives/" title="归档">归档</a>
                    
                    <a  href="https://luckly.work/about/" title="关于我">关于我</a>
                    
                    <a  href="https://github.com/ITmxs/" title="github">github</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    <style type="text/css">
    .post-toc {
        position: fixed;
        width: 200px;
        margin-left: -210px;
        padding: 5px 10px;
        font-family: Athelas, STHeiti, Microsoft Yahei, serif;
        font-size: 12px;
        border: 1px solid rgba(0, 0, 0, .07);
        border-radius: 5px;
        background-color: rgba(255, 255, 255, 0.98);
        background-clip: padding-box;
        -webkit-box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        word-wrap: break-word;
        white-space: nowrap;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        z-index: 999;
        cursor: pointer;
        max-height: 70%;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .post-toc .post-toc-title {
        width: 100%;
        margin: 0 auto;
        font-size: 20px;
        font-weight: 400;
        text-transform: uppercase;
        text-align: center;
    }

    .post-toc .post-toc-content {
        font-size: 15px;
    }

    .post-toc .post-toc-content>nav>ul {
        margin: 10px 0;
    }

    .post-toc .post-toc-content ul {
        padding-left: 20px;
        list-style: square;
        margin: 0.5em;
        line-height: 1.8em;
    }

    .post-toc .post-toc-content ul ul {
        padding-left: 15px;
        display: none;
    }

    @media print,
    screen and (max-width:1057px) {
        .post-toc {
            display: none;
        }
    }
</style>
<div class="post-toc" style="position: absolute; top: 188px;">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#查询">查询</a>
      <ul>
        <li><a href="#一般查询">一般查询</a></li>
        <li><a href="#where-条件">Where 条件</a></li>
        <li><a href="#not-条件">Not 条件</a></li>
        <li><a href="#or条件">Or条件</a></li>
        <li><a href="#内联条件">内联条件</a></li>
        <li><a href="#额外查询选项">额外查询选项</a></li>
        <li><a href="#firstorinit">FirstOrInit</a></li>
        <li><a href="#firstorcreate">FirstOrCreate</a></li>
        <li><a href="#高级查询">高级查询</a></li>
        <li><a href="#pluck">Pluck</a></li>
        <li><a href="#扫描">扫描</a></li>
      </ul>
    </li>
    <li><a href="#链式操作相关">链式操作相关</a>
      <ul>
        <li><a href="#链式操作">链式操作</a></li>
        <li><a href="#立即执行方法">立即执行方法</a></li>
        <li><a href="#范围">范围</a></li>
        <li><a href="#多个立即执行方法">多个立即执行方法</a></li>
      </ul>
    </li>
    <li><a href="#更新">更新</a>
      <ul>
        <li><a href="#更新所有字段">更新所有字段</a></li>
        <li><a href="#更新修改字段">更新修改字段</a></li>
        <li><a href="#更新选定字段">更新选定字段</a></li>
        <li><a href="#无hooks更新">无Hooks更新</a></li>
        <li><a href="#批量更新">批量更新</a></li>
        <li><a href="#使用sql表达式更新">使用SQL表达式更新</a></li>
        <li><a href="#修改hooks中的值">修改Hooks中的值</a></li>
        <li><a href="#其它更新选项">其它更新选项</a></li>
      </ul>
    </li>
    <li><a href="#删除">删除</a>
      <ul>
        <li><a href="#删除记录">删除记录</a></li>
        <li><a href="#批量删除">批量删除</a></li>
        <li><a href="#软删除">软删除</a></li>
        <li><a href="#物理删除">物理删除</a></li>
      </ul>
    </li>
  </ul>
</nav>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var postToc = $(".post-toc");
        if (postToc.length) {
            var leftPos = $("#main").offset().left;
            if(leftPos<220){
                postToc.css({"width":leftPos-10,"margin-left":(0-leftPos)})
            }

            var t = postToc.offset().top - 20,
                a = {
                    start: {
                        position: "absolute",
                        top: t
                    },
                    process: {
                        position: "fixed",
                        top: 20
                    },
                };
            $(window).scroll(function () {
                var e = $(window).scrollTop();
                e < t ? postToc.css(a.start) : postToc.css(a.process)
            })
        }
    })
</script>
    <article class="post">
        <header>
            <h1 class="post-title">gorm的查询</h1>
        </header>
        <date class="post-meta meta-date">
            2021年9月6日
        </date>
        
        <div class="post-meta">
            <span>|</span>
            
            <span class="meta-category"><a href='/categories/go'>go</a></span>
            
            <span class="meta-category"><a href='/categories/gorm'>gorm</a></span>
            
        </div>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="post-content">
            <p>首先要和数据库成功的建立连接，建立连接，分为三个部分</p>
<pre><code>package main

import (&quot;github.com/jinzhu/gorm&quot;
	_ &quot;github.com/jinzhu/gorm/dialects/mysql&quot;)

type User struct {
	gorm.Model
	//ID        uint `gorm:&quot;primary_key&quot;`
	//	CreatedAt time.Time
	//	UpdatedAt time.Time
	//	DeletedAt *time.Time `sql:&quot;index&quot;
	Name         string
	Age          int64
}

func main()  {
	//1.连接MySQL数据库
	db, err := gorm.Open(&quot;mysql&quot;, &quot;root:123456@(127.0.0.1:3306)/db1?charset=utf8mb4&amp;parseTime=True&amp;loc=Local&quot;)
	if err!=nil{
		panic(err)
	}
	defer db.Close()
	//2.模型与数据中的表连接
	db.AutoMigrate(&amp;User{})
	//3.创建
	u1:= User{Name: &quot;itmxs&quot;, Age: 18}
	db.Create(&amp;u1)
	u2:= User{Name: &quot;honey&quot;, Age: 19}
	db.Create(&amp;u2)
	//4.查询

}

</code></pre><p>接下来开始查询操作：</p>
<pre><code>//4.查询
	//var user User  //声明模型结构体类型变量uesr(在A文件夹里)
	// 根据主键查询第一条记录
	user := new(User) //new和Make的区别
	db.First(&amp;user)   //(在B文件夹里)
	//// SELECT * FROM users ORDER BY id LIMIT 1;
	//fmt.Printf(&quot;user:%#v\n&quot;, user)
	var users []User
	//db.Debug().Find(&amp;users)

	// Map
	db.Where(map[string]interface{}{&quot;name&quot;: &quot;itmxs&quot;, &quot;age&quot;: 18}).Find(&amp;users)
	//// SELECT * FROM users WHERE name = &quot;jinzhu&quot; AND age = 20;
	fmt.Printf(&quot;users:%#v\n&quot;, users)
</code></pre><h2 id="查询">查询</h2>
<h3 id="一般查询">一般查询</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 根据主键查询第一条记录
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users ORDER BY id LIMIT 1;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 随机获取一条记录
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Take</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users LIMIT 1;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 根据主键查询最后一条记录
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Last</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users ORDER BY id DESC LIMIT 1;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 查询所有的记录
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 查询指定的某条记录(仅当主键为整型时可用)
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>, <span style="color:#ae81ff">10</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE id = 10;
</span></code></pre></div><h3 id="where-条件">Where 条件</h3>
<h4 id="普通sql查询">普通SQL查询</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// Get first matched record
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name = ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu&#34;</span>).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#39;jinzhu&#39; limit 1;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Get all matched records
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name = ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#39;jinzhu&#39;;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// &lt;&gt;
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name &lt;&gt; ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name &lt;&gt; &#39;jinzhu&#39;;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// IN
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name IN (?)&#34;</span>, []<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;jinzhu&#34;</span>, <span style="color:#e6db74">&#34;jinzhu 2&#34;</span>}).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name in (&#39;jinzhu&#39;,&#39;jinzhu 2&#39;);
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// LIKE
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name LIKE ?&#34;</span>, <span style="color:#e6db74">&#34;%jin%&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name LIKE &#39;%jin%&#39;;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// AND
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name = ? AND age &gt;= ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu&#34;</span>, <span style="color:#e6db74">&#34;22&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#39;jinzhu&#39; AND age &gt;= 22;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Time
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;updated_at &gt; ?&#34;</span>, <span style="color:#a6e22e">lastWeek</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE updated_at &gt; &#39;2000-01-01 00:00:00&#39;;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// BETWEEN
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;created_at BETWEEN ? AND ?&#34;</span>, <span style="color:#a6e22e">lastWeek</span>, <span style="color:#a6e22e">today</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE created_at BETWEEN &#39;2000-01-01 00:00:00&#39; AND &#39;2000-01-08 00:00:00&#39;;
</span></code></pre></div><h4 id="struct--map查询">Struct &amp; Map查询</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// Struct
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;jinzhu&#34;</span>, <span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">20</span>}).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#34;jinzhu&#34; AND age = 20 LIMIT 1;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Map
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{}{<span style="color:#e6db74">&#34;name&#34;</span>: <span style="color:#e6db74">&#34;jinzhu&#34;</span>, <span style="color:#e6db74">&#34;age&#34;</span>: <span style="color:#ae81ff">20</span>}).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#34;jinzhu&#34; AND age = 20;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 主键的切片
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>([]<span style="color:#66d9ef">int64</span>{<span style="color:#ae81ff">20</span>, <span style="color:#ae81ff">21</span>, <span style="color:#ae81ff">22</span>}).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE id IN (20, 21, 22);
</span></code></pre></div><p>**提示：**当通过结构体进行查询时，GORM将会只通过非零值字段查询，这意味着如果你的字段值为<code>0</code>，<code>''</code>，<code>false</code>或者其他<code>零值</code>时，将不会被用于构建查询条件，例如：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;jinzhu&#34;</span>, <span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">0</span>}).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#34;jinzhu&#34;;
</span></code></pre></div><p>你可以使用指针或实现 Scanner/Valuer 接口来避免这个问题.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 使用指针
</span><span style="color:#75715e"></span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">User</span> <span style="color:#66d9ef">struct</span> {
  <span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Model</span>
  <span style="color:#a6e22e">Name</span> <span style="color:#66d9ef">string</span>
  <span style="color:#a6e22e">Age</span>  <span style="color:#f92672">*</span><span style="color:#66d9ef">int</span>
}

<span style="color:#75715e">// 使用 Scanner/Valuer
</span><span style="color:#75715e"></span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">User</span> <span style="color:#66d9ef">struct</span> {
  <span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Model</span>
  <span style="color:#a6e22e">Name</span> <span style="color:#66d9ef">string</span>
  <span style="color:#a6e22e">Age</span>  <span style="color:#a6e22e">sql</span>.<span style="color:#a6e22e">NullInt64</span>  <span style="color:#75715e">// sql.NullInt64 实现了 Scanner/Valuer 接口
</span><span style="color:#75715e"></span>}
</code></pre></div><h3 id="not-条件">Not 条件</h3>
<p>作用与 Where 类似的情形如下：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Not</span>(<span style="color:#e6db74">&#34;name&#34;</span>, <span style="color:#e6db74">&#34;jinzhu&#34;</span>).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name &lt;&gt; &#34;jinzhu&#34; LIMIT 1;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Not In
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Not</span>(<span style="color:#e6db74">&#34;name&#34;</span>, []<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;jinzhu&#34;</span>, <span style="color:#e6db74">&#34;jinzhu 2&#34;</span>}).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name NOT IN (&#34;jinzhu&#34;, &#34;jinzhu 2&#34;);
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Not In slice of primary keys
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Not</span>([]<span style="color:#66d9ef">int64</span>{<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>}).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE id NOT IN (1,2,3);
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Not</span>([]<span style="color:#66d9ef">int64</span>{}).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Plain SQL
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Not</span>(<span style="color:#e6db74">&#34;name = ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu&#34;</span>).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE NOT(name = &#34;jinzhu&#34;);
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Struct
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Not</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;jinzhu&#34;</span>}).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name &lt;&gt; &#34;jinzhu&#34;;
</span></code></pre></div><h3 id="or条件">Or条件</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;role = ?&#34;</span>, <span style="color:#e6db74">&#34;admin&#34;</span>).<span style="color:#a6e22e">Or</span>(<span style="color:#e6db74">&#34;role = ?&#34;</span>, <span style="color:#e6db74">&#34;super_admin&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE role = &#39;admin&#39; OR role = &#39;super_admin&#39;;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Struct
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name = &#39;jinzhu&#39;&#34;</span>).<span style="color:#a6e22e">Or</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;jinzhu 2&#34;</span>}).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#39;jinzhu&#39; OR name = &#39;jinzhu 2&#39;;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Map
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name = &#39;jinzhu&#39;&#34;</span>).<span style="color:#a6e22e">Or</span>(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{}{<span style="color:#e6db74">&#34;name&#34;</span>: <span style="color:#e6db74">&#34;jinzhu 2&#34;</span>}).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#39;jinzhu&#39; OR name = &#39;jinzhu 2&#39;;
</span></code></pre></div><h3 id="内联条件">内联条件</h3>
<p>作用与<code>Where</code>查询类似，当内联条件与多个<a href="https://www.liwenzhou.com/posts/Go/gorm_crud/#autoid-1-3-1">立即执行方法</a>一起使用时, 内联条件不会传递给后面的立即执行方法。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 根据主键获取记录 (只适用于整形主键)
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>, <span style="color:#ae81ff">23</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE id = 23 LIMIT 1;
</span><span style="color:#75715e">// 根据主键获取记录, 如果它是一个非整形主键
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>, <span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#e6db74">&#34;string_primary_key&#34;</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE id = &#39;string_primary_key&#39; LIMIT 1;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Plain SQL
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>, <span style="color:#e6db74">&#34;name = ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu&#34;</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#34;jinzhu&#34;;
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>, <span style="color:#e6db74">&#34;name &lt;&gt; ? AND age &gt; ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu&#34;</span>, <span style="color:#ae81ff">20</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name &lt;&gt; &#34;jinzhu&#34; AND age &gt; 20;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Struct
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>, <span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">20</span>})
<span style="color:#75715e">//// SELECT * FROM users WHERE age = 20;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Map
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>, <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{}{<span style="color:#e6db74">&#34;age&#34;</span>: <span style="color:#ae81ff">20</span>})
<span style="color:#75715e">//// SELECT * FROM users WHERE age = 20;
</span></code></pre></div><h3 id="额外查询选项">额外查询选项</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 为查询 SQL 添加额外的 SQL 操作
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Set</span>(<span style="color:#e6db74">&#34;gorm:query_option&#34;</span>, <span style="color:#e6db74">&#34;FOR UPDATE&#34;</span>).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>, <span style="color:#ae81ff">10</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE id = 10 FOR UPDATE;
</span></code></pre></div><h3 id="firstorinit">FirstOrInit</h3>
<p>获取匹配的第一条记录，否则根据给定的条件初始化一个新的对象 (仅支持 struct 和 map 条件)</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 未找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">FirstOrInit</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>, <span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;non_existing&#34;</span>})
<span style="color:#75715e">//// user -&gt; User{Name: &#34;non_existing&#34;}
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;Jinzhu&#34;</span>}).<span style="color:#a6e22e">FirstOrInit</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// user -&gt; User{Id: 111, Name: &#34;Jinzhu&#34;, Age: 20}
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">FirstOrInit</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>, <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{}{<span style="color:#e6db74">&#34;name&#34;</span>: <span style="color:#e6db74">&#34;jinzhu&#34;</span>})
<span style="color:#75715e">//// user -&gt; User{Id: 111, Name: &#34;Jinzhu&#34;, Age: 20}
</span></code></pre></div><h4 id="attrs">Attrs</h4>
<p>如果记录未找到，将使用参数初始化 struct.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 未找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;non_existing&#34;</span>}).<span style="color:#a6e22e">Attrs</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">20</span>}).<span style="color:#a6e22e">FirstOrInit</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM USERS WHERE name = &#39;non_existing&#39;;
</span><span style="color:#75715e">//// user -&gt; User{Name: &#34;non_existing&#34;, Age: 20}
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;non_existing&#34;</span>}).<span style="color:#a6e22e">Attrs</span>(<span style="color:#e6db74">&#34;age&#34;</span>, <span style="color:#ae81ff">20</span>).<span style="color:#a6e22e">FirstOrInit</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM USERS WHERE name = &#39;non_existing&#39;;
</span><span style="color:#75715e">//// user -&gt; User{Name: &#34;non_existing&#34;, Age: 20}
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;Jinzhu&#34;</span>}).<span style="color:#a6e22e">Attrs</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">30</span>}).<span style="color:#a6e22e">FirstOrInit</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM USERS WHERE name = jinzhu&#39;;
</span><span style="color:#75715e">//// user -&gt; User{Id: 111, Name: &#34;Jinzhu&#34;, Age: 20}
</span></code></pre></div><h4 id="assign">Assign</h4>
<p>不管记录是否找到，都将参数赋值给 struct.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 未找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;non_existing&#34;</span>}).<span style="color:#a6e22e">Assign</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">20</span>}).<span style="color:#a6e22e">FirstOrInit</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// user -&gt; User{Name: &#34;non_existing&#34;, Age: 20}
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;Jinzhu&#34;</span>}).<span style="color:#a6e22e">Assign</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">30</span>}).<span style="color:#a6e22e">FirstOrInit</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM USERS WHERE name = jinzhu&#39;;
</span><span style="color:#75715e">//// user -&gt; User{Id: 111, Name: &#34;Jinzhu&#34;, Age: 30}
</span></code></pre></div><h3 id="firstorcreate">FirstOrCreate</h3>
<p>获取匹配的第一条记录, 否则根据给定的条件创建一个新的记录 (仅支持 struct 和 map 条件)</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 未找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">FirstOrCreate</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>, <span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;non_existing&#34;</span>})
<span style="color:#75715e">//// INSERT INTO &#34;users&#34; (name) VALUES (&#34;non_existing&#34;);
</span><span style="color:#75715e">//// user -&gt; User{Id: 112, Name: &#34;non_existing&#34;}
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;Jinzhu&#34;</span>}).<span style="color:#a6e22e">FirstOrCreate</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// user -&gt; User{Id: 111, Name: &#34;Jinzhu&#34;}
</span></code></pre></div><h4 id="attrs-1">Attrs</h4>
<p>如果记录未找到，将使用参数创建 struct 和记录.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"> <span style="color:#75715e">// 未找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;non_existing&#34;</span>}).<span style="color:#a6e22e">Attrs</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">20</span>}).<span style="color:#a6e22e">FirstOrCreate</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#39;non_existing&#39;;
</span><span style="color:#75715e">//// INSERT INTO &#34;users&#34; (name, age) VALUES (&#34;non_existing&#34;, 20);
</span><span style="color:#75715e">//// user -&gt; User{Id: 112, Name: &#34;non_existing&#34;, Age: 20}
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;jinzhu&#34;</span>}).<span style="color:#a6e22e">Attrs</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">30</span>}).<span style="color:#a6e22e">FirstOrCreate</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#39;jinzhu&#39;;
</span><span style="color:#75715e">//// user -&gt; User{Id: 111, Name: &#34;jinzhu&#34;, Age: 20}
</span></code></pre></div><h4 id="assign-1">Assign</h4>
<p>不管记录是否找到，都将参数赋值给 struct 并保存至数据库.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 未找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;non_existing&#34;</span>}).<span style="color:#a6e22e">Assign</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">20</span>}).<span style="color:#a6e22e">FirstOrCreate</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#39;non_existing&#39;;
</span><span style="color:#75715e">//// INSERT INTO &#34;users&#34; (name, age) VALUES (&#34;non_existing&#34;, 20);
</span><span style="color:#75715e">//// user -&gt; User{Id: 112, Name: &#34;non_existing&#34;, Age: 20}
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 找到
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;jinzhu&#34;</span>}).<span style="color:#a6e22e">Assign</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">30</span>}).<span style="color:#a6e22e">FirstOrCreate</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE name = &#39;jinzhu&#39;;
</span><span style="color:#75715e">//// UPDATE users SET age=30 WHERE id = 111;
</span><span style="color:#75715e">//// user -&gt; User{Id: 111, Name: &#34;jinzhu&#34;, Age: 30}
</span></code></pre></div><h3 id="高级查询">高级查询</h3>
<h4 id="子查询">子查询</h4>
<p>基于 <code>*gorm.expr</code> 的子查询</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;amount &gt; ?&#34;</span>, <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;orders&#34;</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;AVG(amount)&#34;</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;state = ?&#34;</span>, <span style="color:#e6db74">&#34;paid&#34;</span>).<span style="color:#a6e22e">SubQuery</span>()).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">orders</span>)
<span style="color:#75715e">// SELECT * FROM &#34;orders&#34;  WHERE &#34;orders&#34;.&#34;deleted_at&#34; IS NULL AND (amount &gt; (SELECT AVG(amount) FROM &#34;orders&#34;  WHERE (state = &#39;paid&#39;)));
</span></code></pre></div><h4 id="选择字段">选择字段</h4>
<p>Select，指定你想从数据库中检索出的字段，默认会选择全部字段。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;name, age&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT name, age FROM users;
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Select</span>([]<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;name&#34;</span>, <span style="color:#e6db74">&#34;age&#34;</span>}).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT name, age FROM users;
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;users&#34;</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;COALESCE(age,?)&#34;</span>, <span style="color:#ae81ff">42</span>).<span style="color:#a6e22e">Rows</span>()
<span style="color:#75715e">//// SELECT COALESCE(age,&#39;42&#39;) FROM users;
</span></code></pre></div><h4 id="排序">排序</h4>
<p>Order，指定从数据库中检索出记录的顺序。设置第二个参数 reorder 为 <code>true</code> ，可以覆盖前面定义的排序条件。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Order</span>(<span style="color:#e6db74">&#34;age desc, name&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users ORDER BY age desc, name;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 多字段排序
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Order</span>(<span style="color:#e6db74">&#34;age desc&#34;</span>).<span style="color:#a6e22e">Order</span>(<span style="color:#e6db74">&#34;name&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users ORDER BY age desc, name;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 覆盖排序
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Order</span>(<span style="color:#e6db74">&#34;age desc&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users1</span>).<span style="color:#a6e22e">Order</span>(<span style="color:#e6db74">&#34;age&#34;</span>, <span style="color:#66d9ef">true</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users2</span>)
<span style="color:#75715e">//// SELECT * FROM users ORDER BY age desc; (users1)
</span><span style="color:#75715e">//// SELECT * FROM users ORDER BY age; (users2)
</span></code></pre></div><h4 id="数量">数量</h4>
<p>Limit，指定从数据库检索出的最大记录数。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Limit</span>(<span style="color:#ae81ff">3</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users LIMIT 3;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// -1 取消 Limit 条件
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Limit</span>(<span style="color:#ae81ff">10</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users1</span>).<span style="color:#a6e22e">Limit</span>(<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users2</span>)
<span style="color:#75715e">//// SELECT * FROM users LIMIT 10; (users1)
</span><span style="color:#75715e">//// SELECT * FROM users; (users2)
</span></code></pre></div><h4 id="偏移">偏移</h4>
<p>Offset，指定开始返回记录前要跳过的记录数。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Offset</span>(<span style="color:#ae81ff">3</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users OFFSET 3;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// -1 取消 Offset 条件
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Offset</span>(<span style="color:#ae81ff">10</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users1</span>).<span style="color:#a6e22e">Offset</span>(<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users2</span>)
<span style="color:#75715e">//// SELECT * FROM users OFFSET 10; (users1)
</span><span style="color:#75715e">//// SELECT * FROM users; (users2)
</span></code></pre></div><h4 id="总数">总数</h4>
<p>Count，该 model 能获取的记录总数。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name = ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu&#34;</span>).<span style="color:#a6e22e">Or</span>(<span style="color:#e6db74">&#34;name = ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu 2&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>).<span style="color:#a6e22e">Count</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">count</span>)
<span style="color:#75715e">//// SELECT * from USERS WHERE name = &#39;jinzhu&#39; OR name = &#39;jinzhu 2&#39;; (users)
</span><span style="color:#75715e">//// SELECT count(*) FROM users WHERE name = &#39;jinzhu&#39; OR name = &#39;jinzhu 2&#39;; (count)
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name = ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu&#34;</span>).<span style="color:#a6e22e">Count</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">count</span>)
<span style="color:#75715e">//// SELECT count(*) FROM users WHERE name = &#39;jinzhu&#39;; (count)
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;deleted_users&#34;</span>).<span style="color:#a6e22e">Count</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">count</span>)
<span style="color:#75715e">//// SELECT count(*) FROM deleted_users;
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;deleted_users&#34;</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;count(distinct(name))&#34;</span>).<span style="color:#a6e22e">Count</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">count</span>)
<span style="color:#75715e">//// SELECT count( distinct(name) ) FROM deleted_users; (count)
</span></code></pre></div><p><strong>注意</strong> <code>Count</code> 必须是链式查询的最后一个操作 ，因为它会覆盖前面的 <code>SELECT</code>，但如果里面使用了 <code>count</code> 时不会覆盖</p>
<h4 id="group--having">Group &amp; Having</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">rows</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;orders&#34;</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;date(created_at) as date, sum(amount) as total&#34;</span>).<span style="color:#a6e22e">Group</span>(<span style="color:#e6db74">&#34;date(created_at)&#34;</span>).<span style="color:#a6e22e">Rows</span>()
<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">rows</span>.<span style="color:#a6e22e">Next</span>() {
  <span style="color:#f92672">...</span>
}

<span style="color:#75715e">// 使用Scan将多条结果扫描进事先准备好的结构体切片中
</span><span style="color:#75715e"></span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Result</span> <span style="color:#66d9ef">struct</span> {
	<span style="color:#a6e22e">Date</span> <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Time</span>
	<span style="color:#a6e22e">Total</span> <span style="color:#66d9ef">int</span>
}
<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">rets</span> []<span style="color:#a6e22e">Result</span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;users&#34;</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;date(created_at) as date, sum(age) as total&#34;</span>).<span style="color:#a6e22e">Group</span>(<span style="color:#e6db74">&#34;date(created_at)&#34;</span>).<span style="color:#a6e22e">Scan</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">rets</span>)

<span style="color:#a6e22e">rows</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;orders&#34;</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;date(created_at) as date, sum(amount) as total&#34;</span>).<span style="color:#a6e22e">Group</span>(<span style="color:#e6db74">&#34;date(created_at)&#34;</span>).<span style="color:#a6e22e">Having</span>(<span style="color:#e6db74">&#34;sum(amount) &gt; ?&#34;</span>, <span style="color:#ae81ff">100</span>).<span style="color:#a6e22e">Rows</span>()
<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">rows</span>.<span style="color:#a6e22e">Next</span>() {
  <span style="color:#f92672">...</span>
}

<span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Result</span> <span style="color:#66d9ef">struct</span> {
  <span style="color:#a6e22e">Date</span>  <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Time</span>
  <span style="color:#a6e22e">Total</span> <span style="color:#66d9ef">int64</span>
}
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;orders&#34;</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;date(created_at) as date, sum(amount) as total&#34;</span>).<span style="color:#a6e22e">Group</span>(<span style="color:#e6db74">&#34;date(created_at)&#34;</span>).<span style="color:#a6e22e">Having</span>(<span style="color:#e6db74">&#34;sum(amount) &gt; ?&#34;</span>, <span style="color:#ae81ff">100</span>).<span style="color:#a6e22e">Scan</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">results</span>)
</code></pre></div><h4 id="连接">连接</h4>
<p>Joins，指定连接条件</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">rows</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;users&#34;</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;users.name, emails.email&#34;</span>).<span style="color:#a6e22e">Joins</span>(<span style="color:#e6db74">&#34;left join emails on emails.user_id = users.id&#34;</span>).<span style="color:#a6e22e">Rows</span>()
<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">rows</span>.<span style="color:#a6e22e">Next</span>() {
  <span style="color:#f92672">...</span>
}

<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;users&#34;</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;users.name, emails.email&#34;</span>).<span style="color:#a6e22e">Joins</span>(<span style="color:#e6db74">&#34;left join emails on emails.user_id = users.id&#34;</span>).<span style="color:#a6e22e">Scan</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">results</span>)

<span style="color:#75715e">// 多连接及参数
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Joins</span>(<span style="color:#e6db74">&#34;JOIN emails ON emails.user_id = users.id AND emails.email = ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu@example.org&#34;</span>).<span style="color:#a6e22e">Joins</span>(<span style="color:#e6db74">&#34;JOIN credit_cards ON credit_cards.user_id = users.id&#34;</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;credit_cards.number = ?&#34;</span>, <span style="color:#e6db74">&#34;411111111111&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
</code></pre></div><h3 id="pluck">Pluck</h3>
<p>Pluck，查询 model 中的一个列作为切片，如果您想要查询多个列，您应该使用 <a href="https://www.liwenzhou.com/posts/Go/gorm_crud/#Scan"><code>Scan</code></a></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">ages</span> []<span style="color:#66d9ef">int64</span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>).<span style="color:#a6e22e">Pluck</span>(<span style="color:#e6db74">&#34;age&#34;</span>, <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">ages</span>)

<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">names</span> []<span style="color:#66d9ef">string</span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Pluck</span>(<span style="color:#e6db74">&#34;name&#34;</span>, <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">names</span>)

<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;deleted_users&#34;</span>).<span style="color:#a6e22e">Pluck</span>(<span style="color:#e6db74">&#34;name&#34;</span>, <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">names</span>)

<span style="color:#75715e">// 想查询多个字段？ 这样做：
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;name, age&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
</code></pre></div><h3 id="扫描">扫描</h3>
<p>Scan，扫描结果至一个 struct.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Result</span> <span style="color:#66d9ef">struct</span> {
  <span style="color:#a6e22e">Name</span> <span style="color:#66d9ef">string</span>
  <span style="color:#a6e22e">Age</span>  <span style="color:#66d9ef">int</span>
}

<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">result</span> <span style="color:#a6e22e">Result</span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;users&#34;</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;name, age&#34;</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name = ?&#34;</span>, <span style="color:#e6db74">&#34;Antonio&#34;</span>).<span style="color:#a6e22e">Scan</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">result</span>)

<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">results</span> []<span style="color:#a6e22e">Result</span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;users&#34;</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;name, age&#34;</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id &gt; ?&#34;</span>, <span style="color:#ae81ff">0</span>).<span style="color:#a6e22e">Scan</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">results</span>)

<span style="color:#75715e">// 原生 SQL
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Raw</span>(<span style="color:#e6db74">&#34;SELECT name, age FROM users WHERE name = ?&#34;</span>, <span style="color:#e6db74">&#34;Antonio&#34;</span>).<span style="color:#a6e22e">Scan</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">result</span>)
</code></pre></div><h2 id="链式操作相关">链式操作相关</h2>
<h3 id="链式操作">链式操作</h3>
<p>Method Chaining，Gorm 实现了链式操作接口，所以你可以把代码写成这样：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 创建一个查询
</span><span style="color:#75715e"></span><span style="color:#a6e22e">tx</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name = ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu&#34;</span>)

<span style="color:#75715e">// 添加更多条件
</span><span style="color:#75715e"></span><span style="color:#66d9ef">if</span> <span style="color:#a6e22e">someCondition</span> {
  <span style="color:#a6e22e">tx</span> = <span style="color:#a6e22e">tx</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;age = ?&#34;</span>, <span style="color:#ae81ff">20</span>)
} <span style="color:#66d9ef">else</span> {
  <span style="color:#a6e22e">tx</span> = <span style="color:#a6e22e">tx</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;age = ?&#34;</span>, <span style="color:#ae81ff">30</span>)
}

<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">yetAnotherCondition</span> {
  <span style="color:#a6e22e">tx</span> = <span style="color:#a6e22e">tx</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;active = ?&#34;</span>, <span style="color:#ae81ff">1</span>)
}
</code></pre></div><p>在调用立即执行方法前不会生成<code>Query</code>语句，借助这个特性你可以创建一个函数来处理一些通用逻辑。</p>
<h3 id="立即执行方法">立即执行方法</h3>
<p>Immediate methods ，立即执行方法是指那些会立即生成<code>SQL</code>语句并发送到数据库的方法, 他们一般是<code>CRUD</code>方法，比如：</p>
<p><code>Create</code>, <code>First</code>, <code>Find</code>, <code>Take</code>, <code>Save</code>, <code>UpdateXXX</code>, <code>Delete</code>, <code>Scan</code>, <code>Row</code>, <code>Rows</code>…</p>
<p>这有一个基于上面链式方法代码的立即执行方法的例子：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">tx</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
</code></pre></div><p>生成的SQL语句如下：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">SELECT</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">FROM</span> users <span style="color:#66d9ef">where</span> name <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;jinzhu&#39;</span> <span style="color:#66d9ef">AND</span> age <span style="color:#f92672">=</span> <span style="color:#ae81ff">30</span> <span style="color:#66d9ef">AND</span> active <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>;
</code></pre></div><h3 id="范围">范围</h3>
<p><code>Scopes</code>，Scope是建立在链式操作的基础之上的。</p>
<p>基于它，你可以抽取一些通用逻辑，写出更多可重用的函数库。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">AmountGreaterThan1000</span>(<span style="color:#a6e22e">db</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span>) <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span> {
  <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;amount &gt; ?&#34;</span>, <span style="color:#ae81ff">1000</span>)
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">PaidWithCreditCard</span>(<span style="color:#a6e22e">db</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span>) <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span> {
  <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;pay_mode_sign = ?&#34;</span>, <span style="color:#e6db74">&#34;C&#34;</span>)
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">PaidWithCod</span>(<span style="color:#a6e22e">db</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span>) <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span> {
  <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;pay_mode_sign = ?&#34;</span>, <span style="color:#e6db74">&#34;C&#34;</span>)
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">OrderStatus</span>(<span style="color:#a6e22e">status</span> []<span style="color:#66d9ef">string</span>) <span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">db</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span>) <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span> {
  <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">db</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span>) <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">DB</span> {
    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Scopes</span>(<span style="color:#a6e22e">AmountGreaterThan1000</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;status IN (?)&#34;</span>, <span style="color:#a6e22e">status</span>)
  }
}

<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Scopes</span>(<span style="color:#a6e22e">AmountGreaterThan1000</span>, <span style="color:#a6e22e">PaidWithCreditCard</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">orders</span>)
<span style="color:#75715e">// 查找所有金额大于 1000 的信用卡订单
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Scopes</span>(<span style="color:#a6e22e">AmountGreaterThan1000</span>, <span style="color:#a6e22e">PaidWithCod</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">orders</span>)
<span style="color:#75715e">// 查找所有金额大于 1000 的 COD 订单
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Scopes</span>(<span style="color:#a6e22e">AmountGreaterThan1000</span>, <span style="color:#a6e22e">OrderStatus</span>([]<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;paid&#34;</span>, <span style="color:#e6db74">&#34;shipped&#34;</span>})).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">orders</span>)
<span style="color:#75715e">// 查找所有金额大于 1000 且已付款或者已发货的订单
</span></code></pre></div><h3 id="多个立即执行方法">多个立即执行方法</h3>
<p>Multiple Immediate Methods，在 GORM 中使用多个立即执行方法时，后一个立即执行方法会复用前一个<strong>立即执行方法</strong>的条件 (不包括内联条件) 。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name LIKE ?&#34;</span>, <span style="color:#e6db74">&#34;jinzhu%&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>, <span style="color:#e6db74">&#34;id IN (?)&#34;</span>, []<span style="color:#66d9ef">int</span>{<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>}).<span style="color:#a6e22e">Count</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">count</span>)
</code></pre></div><p>生成的 Sql</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">SELECT</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">FROM</span> users <span style="color:#66d9ef">WHERE</span> name <span style="color:#66d9ef">LIKE</span> <span style="color:#e6db74">&#39;jinzhu%&#39;</span> <span style="color:#66d9ef">AND</span> id <span style="color:#66d9ef">IN</span> (<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>)

<span style="color:#66d9ef">SELECT</span> <span style="color:#66d9ef">count</span>(<span style="color:#f92672">*</span>) <span style="color:#66d9ef">FROM</span> users <span style="color:#66d9ef">WHERE</span> name <span style="color:#66d9ef">LIKE</span> <span style="color:#e6db74">&#39;jinzhu%&#39;</span>
</code></pre></div><h2 id="更新">更新</h2>
<h3 id="更新所有字段">更新所有字段</h3>
<p><code>Save()</code>默认会更新该对象的所有字段，即使你没有赋值。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)

<span style="color:#a6e22e">user</span>.<span style="color:#a6e22e">Name</span> = <span style="color:#e6db74">&#34;七米&#34;</span>
<span style="color:#a6e22e">user</span>.<span style="color:#a6e22e">Age</span> = <span style="color:#ae81ff">99</span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Save</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)

<span style="color:#75715e">////  UPDATE `users` SET `created_at` = &#39;2020-02-16 12:52:20&#39;, `updated_at` = &#39;2020-02-16 12:54:55&#39;, `deleted_at` = NULL, `name` = &#39;七米&#39;, `age` = 99, `active` = true  WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 1
</span></code></pre></div><h3 id="更新修改字段">更新修改字段</h3>
<p>如果你只希望更新指定字段，可以使用<code>Update</code>或者<code>Updates</code></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 更新单个属性，如果它有变化
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Update</span>(<span style="color:#e6db74">&#34;name&#34;</span>, <span style="color:#e6db74">&#34;hello&#34;</span>)
<span style="color:#75715e">//// UPDATE users SET name=&#39;hello&#39;, updated_at=&#39;2013-11-17 21:34:10&#39; WHERE id=111;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 根据给定的条件更新单个属性
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;active = ?&#34;</span>, <span style="color:#66d9ef">true</span>).<span style="color:#a6e22e">Update</span>(<span style="color:#e6db74">&#34;name&#34;</span>, <span style="color:#e6db74">&#34;hello&#34;</span>)
<span style="color:#75715e">//// UPDATE users SET name=&#39;hello&#39;, updated_at=&#39;2013-11-17 21:34:10&#39; WHERE id=111 AND active=true;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 使用 map 更新多个属性，只会更新其中有变化的属性
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Updates</span>(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{}{<span style="color:#e6db74">&#34;name&#34;</span>: <span style="color:#e6db74">&#34;hello&#34;</span>, <span style="color:#e6db74">&#34;age&#34;</span>: <span style="color:#ae81ff">18</span>, <span style="color:#e6db74">&#34;active&#34;</span>: <span style="color:#66d9ef">false</span>})
<span style="color:#75715e">//// UPDATE users SET name=&#39;hello&#39;, age=18, active=false, updated_at=&#39;2013-11-17 21:34:10&#39; WHERE id=111;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 使用 struct 更新多个属性，只会更新其中有变化且为非零值的字段
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Updates</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;hello&#34;</span>, <span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">18</span>})
<span style="color:#75715e">//// UPDATE users SET name=&#39;hello&#39;, age=18, updated_at = &#39;2013-11-17 21:34:10&#39; WHERE id = 111;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 警告：当使用 struct 更新时，GORM只会更新那些非零值的字段
</span><span style="color:#75715e">// 对于下面的操作，不会发生任何更新，&#34;&#34;, 0, false 都是其类型的零值
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Updates</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">0</span>, <span style="color:#a6e22e">Active</span>: <span style="color:#66d9ef">false</span>})
</code></pre></div><h3 id="更新选定字段">更新选定字段</h3>
<p>如果你想更新或忽略某些字段，你可以使用 <code>Select</code>，<code>Omit</code></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;name&#34;</span>).<span style="color:#a6e22e">Updates</span>(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{}{<span style="color:#e6db74">&#34;name&#34;</span>: <span style="color:#e6db74">&#34;hello&#34;</span>, <span style="color:#e6db74">&#34;age&#34;</span>: <span style="color:#ae81ff">18</span>, <span style="color:#e6db74">&#34;active&#34;</span>: <span style="color:#66d9ef">false</span>})
<span style="color:#75715e">//// UPDATE users SET name=&#39;hello&#39;, updated_at=&#39;2013-11-17 21:34:10&#39; WHERE id=111;
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Omit</span>(<span style="color:#e6db74">&#34;name&#34;</span>).<span style="color:#a6e22e">Updates</span>(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{}{<span style="color:#e6db74">&#34;name&#34;</span>: <span style="color:#e6db74">&#34;hello&#34;</span>, <span style="color:#e6db74">&#34;age&#34;</span>: <span style="color:#ae81ff">18</span>, <span style="color:#e6db74">&#34;active&#34;</span>: <span style="color:#66d9ef">false</span>})
<span style="color:#75715e">//// UPDATE users SET age=18, active=false, updated_at=&#39;2013-11-17 21:34:10&#39; WHERE id=111;
</span></code></pre></div><h3 id="无hooks更新">无Hooks更新</h3>
<p>上面的更新操作会自动运行 model 的 <code>BeforeUpdate</code>, <code>AfterUpdate</code> 方法，更新 <code>UpdatedAt</code> 时间戳, 在更新时保存其 <code>Associations</code>, 如果你不想调用这些方法，你可以使用 <code>UpdateColumn</code>， <code>UpdateColumns</code></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 更新单个属性，类似于 `Update`
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">UpdateColumn</span>(<span style="color:#e6db74">&#34;name&#34;</span>, <span style="color:#e6db74">&#34;hello&#34;</span>)
<span style="color:#75715e">//// UPDATE users SET name=&#39;hello&#39; WHERE id = 111;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 更新多个属性，类似于 `Updates`
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">UpdateColumns</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;hello&#34;</span>, <span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">18</span>})
<span style="color:#75715e">//// UPDATE users SET name=&#39;hello&#39;, age=18 WHERE id = 111;
</span></code></pre></div><h3 id="批量更新">批量更新</h3>
<p>批量更新时<code>Hooks(钩子函数)</code>不会运行。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Table</span>(<span style="color:#e6db74">&#34;users&#34;</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id IN (?)&#34;</span>, []<span style="color:#66d9ef">int</span>{<span style="color:#ae81ff">10</span>, <span style="color:#ae81ff">11</span>}).<span style="color:#a6e22e">Updates</span>(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{}{<span style="color:#e6db74">&#34;name&#34;</span>: <span style="color:#e6db74">&#34;hello&#34;</span>, <span style="color:#e6db74">&#34;age&#34;</span>: <span style="color:#ae81ff">18</span>})
<span style="color:#75715e">//// UPDATE users SET name=&#39;hello&#39;, age=18 WHERE id IN (10, 11);
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 使用 struct 更新时，只会更新非零值字段，若想更新所有字段，请使用map[string]interface{}
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Updates</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;hello&#34;</span>, <span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">18</span>})
<span style="color:#75715e">//// UPDATE users SET name=&#39;hello&#39;, age=18;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 使用 `RowsAffected` 获取更新记录总数
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#a6e22e">User</span>{}).<span style="color:#a6e22e">Updates</span>(<span style="color:#a6e22e">User</span>{<span style="color:#a6e22e">Name</span>: <span style="color:#e6db74">&#34;hello&#34;</span>, <span style="color:#a6e22e">Age</span>: <span style="color:#ae81ff">18</span>}).<span style="color:#a6e22e">RowsAffected</span>
</code></pre></div><h3 id="使用sql表达式更新">使用SQL表达式更新</h3>
<p>先查询表中的第一条数据保存至user变量。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">user</span> <span style="color:#a6e22e">User</span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Update</span>(<span style="color:#e6db74">&#34;age&#34;</span>, <span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Expr</span>(<span style="color:#e6db74">&#34;age * ? + ?&#34;</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">100</span>))
<span style="color:#75715e">//// UPDATE `users` SET `age` = age * 2 + 100, `updated_at` = &#39;2020-02-16 13:10:20&#39;  WHERE `users`.`id` = 1;
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Updates</span>(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{}{<span style="color:#e6db74">&#34;age&#34;</span>: <span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Expr</span>(<span style="color:#e6db74">&#34;age * ? + ?&#34;</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">100</span>)})
<span style="color:#75715e">//// UPDATE &#34;users&#34; SET &#34;age&#34; = age * &#39;2&#39; + &#39;100&#39;, &#34;updated_at&#34; = &#39;2020-02-16 13:05:51&#39; WHERE `users`.`id` = 1;
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">UpdateColumn</span>(<span style="color:#e6db74">&#34;age&#34;</span>, <span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Expr</span>(<span style="color:#e6db74">&#34;age - ?&#34;</span>, <span style="color:#ae81ff">1</span>))
<span style="color:#75715e">//// UPDATE &#34;users&#34; SET &#34;age&#34; = age - 1 WHERE &#34;id&#34; = &#39;1&#39;;
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;age &gt; 10&#34;</span>).<span style="color:#a6e22e">UpdateColumn</span>(<span style="color:#e6db74">&#34;age&#34;</span>, <span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Expr</span>(<span style="color:#e6db74">&#34;age - ?&#34;</span>, <span style="color:#ae81ff">1</span>))
<span style="color:#75715e">//// UPDATE &#34;users&#34; SET &#34;age&#34; = age - 1 WHERE &#34;id&#34; = &#39;1&#39; AND quantity &gt; 10;
</span></code></pre></div><h3 id="修改hooks中的值">修改Hooks中的值</h3>
<p>如果你想修改 <code>BeforeUpdate</code>, <code>BeforeSave</code> 等 Hooks 中更新的值，你可以使用 <code>scope.SetColumn</code>, 例如：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">user</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">User</span>) <span style="color:#a6e22e">BeforeSave</span>(<span style="color:#a6e22e">scope</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Scope</span>) (<span style="color:#a6e22e">err</span> <span style="color:#66d9ef">error</span>) {
  <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">pw</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">bcrypt</span>.<span style="color:#a6e22e">GenerateFromPassword</span>(<span style="color:#a6e22e">user</span>.<span style="color:#a6e22e">Password</span>, <span style="color:#ae81ff">0</span>); <span style="color:#a6e22e">err</span> <span style="color:#f92672">==</span> <span style="color:#66d9ef">nil</span> {
    <span style="color:#a6e22e">scope</span>.<span style="color:#a6e22e">SetColumn</span>(<span style="color:#e6db74">&#34;EncryptedPassword&#34;</span>, <span style="color:#a6e22e">pw</span>)
  }
}
</code></pre></div><h3 id="其它更新选项">其它更新选项</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 为 update SQL 添加其它的 SQL
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>).<span style="color:#a6e22e">Set</span>(<span style="color:#e6db74">&#34;gorm:update_option&#34;</span>, <span style="color:#e6db74">&#34;OPTION (OPTIMIZE FOR UNKNOWN)&#34;</span>).<span style="color:#a6e22e">Update</span>(<span style="color:#e6db74">&#34;name&#34;</span>, <span style="color:#e6db74">&#34;hello&#34;</span>)
<span style="color:#75715e">//// UPDATE users SET name=&#39;hello&#39;, updated_at = &#39;2013-11-17 21:34:10&#39; WHERE id=111 OPTION (OPTIMIZE FOR UNKNOWN);
</span></code></pre></div><h2 id="删除">删除</h2>
<h3 id="删除记录">删除记录</h3>
<p><strong>警告</strong> 删除记录时，请确保主键字段有值，GORM 会通过主键去删除记录，如果主键为空，GORM 会删除该 model 的所有记录。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 删除现有记录
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Delete</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">email</span>)
<span style="color:#75715e">//// DELETE from emails where id=10;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 为删除 SQL 添加额外的 SQL 操作
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Set</span>(<span style="color:#e6db74">&#34;gorm:delete_option&#34;</span>, <span style="color:#e6db74">&#34;OPTION (OPTIMIZE FOR UNKNOWN)&#34;</span>).<span style="color:#a6e22e">Delete</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">email</span>)
<span style="color:#75715e">//// DELETE from emails where id=10 OPTION (OPTIMIZE FOR UNKNOWN);
</span></code></pre></div><h3 id="批量删除">批量删除</h3>
<p>删除全部匹配的记录</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;email LIKE ?&#34;</span>, <span style="color:#e6db74">&#34;%jinzhu%&#34;</span>).<span style="color:#a6e22e">Delete</span>(<span style="color:#a6e22e">Email</span>{})
<span style="color:#75715e">//// DELETE from emails where email LIKE &#34;%jinzhu%&#34;;
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Delete</span>(<span style="color:#a6e22e">Email</span>{}, <span style="color:#e6db74">&#34;email LIKE ?&#34;</span>, <span style="color:#e6db74">&#34;%jinzhu%&#34;</span>)
<span style="color:#75715e">//// DELETE from emails where email LIKE &#34;%jinzhu%&#34;;
</span></code></pre></div><h3 id="软删除">软删除</h3>
<p>如果一个 model 有 <code>DeletedAt</code> 字段，他将自动获得软删除的功能！ 当调用 <code>Delete</code> 方法时， 记录不会真正的从数据库中被删除， 只会将<code>DeletedAt</code> 字段的值会被设置为当前时间</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Delete</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// UPDATE users SET deleted_at=&#34;2013-10-29 10:23&#34; WHERE id = 111;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 批量删除
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;age = ?&#34;</span>, <span style="color:#ae81ff">20</span>).<span style="color:#a6e22e">Delete</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">User</span>{})
<span style="color:#75715e">//// UPDATE users SET deleted_at=&#34;2013-10-29 10:23&#34; WHERE age = 20;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 查询记录时会忽略被软删除的记录
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;age = 20&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">user</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE age = 20 AND deleted_at IS NULL;
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// Unscoped 方法可以查询被软删除的记录
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Unscoped</span>().<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;age = 20&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">users</span>)
<span style="color:#75715e">//// SELECT * FROM users WHERE age = 20;
</span></code></pre></div><h3 id="物理删除">物理删除</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// Unscoped 方法可以物理删除记录
</span><span style="color:#75715e"></span><span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Unscoped</span>().<span style="color:#a6e22e">Delete</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">order</span>)
<span style="color:#75715e">//// DELETE FROM orders WHERE id=10;
</span></code></pre></div>
        </div>

        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="https://luckly.work/">luckly</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="https://luckly.work/post/go_gorm/CRUD%E7%9A%84%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C/">https://luckly.work/post/go_gorm/CRUD%E7%9A%84%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/post/go_gorm/GORM-CRUD%E6%8C%87%E5%8D%97/">gorm的crud指南</a></li>
        
        <li><a href="/post/go_gorm/gorm%E7%9A%84%E5%AE%9A%E4%B9%89/">gorm的定义</a></li>
        
        <li><a href="/post/go_gorm/%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93/">gorm连接数据库</a></li>
        
        <li><a href="/post/go/go%E7%A0%B4%E8%A7%A3/">Go破解</a></li>
        
        <li><a href="/post/go/go%E5%AE%89%E8%A3%85%E4%B8%8E%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/">Go安装与环境配置</a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            <ul class="clearfix">
                
                <li><a href='/tags/go'>go</a></li>
                
                <li><a href='/tags/gorm'>gorm</a></li>
                
            </ul>
            
        </div>
    </article>
    
    

    
    
    <div class="post bg-white">
      <script src="https://utteranc.es/client.js"
            repo= "https://github.com/ITmxs/repo"
            issue-term="pathname"
            theme="github-light"
            crossorigin="anonymous"
            async>
      </script>
    </div>
    
</div>

                    <footer id="footer">
    <div>
        &copy; 2021 <a href="https://luckly.work/">早起的年轻人 By luckly</a>
        
        | <a rel="nofollow" target="_blank" href="http://beian.miit.gov.cn/">粤ICP备2021号-1</a>
        
    </div>
    <br />
    <div>
        <div class="github-badge">
            <a href="https://juejin.cn/user/3843548384077192" target="_black" rel="nofollow"><span class="badge-subject">Powered by</span><span class="badge-value bg-blue">掘金</span></a>
        </div>
        <div class="github-badge">
            <a href="https://space.bilibili.com/480883651" target="_black"><span class="badge-subject">Design by</span><span class="badge-value bg-brightgreen">早起的年轻人</span></a>
        </div>
        <div class="github-badge">
            <a href="https://cloud.tencent.com/developer/user/6702670" target="_black"><span class="badge-subject">Theme</span><span class="badge-value bg-yellowgreen">云社区</span></a>
        </div>
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script><script src="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/js/totop.js?v=0.0.0' async=""></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/js/douban.js'></script>
    <script src="/js/copy-to-clipboard.js"></script>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='https://luckly.work/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://luckly.work/">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9/" title="Nginx文件内容">Nginx文件内容</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%9E%81%E7%AE%80%E6%95%99%E7%A8%8B/" title="Nginx极简教程">Nginx极简教程</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/%E5%8D%81%E5%88%86%E9%92%9F%E5%85%A5%E9%97%A8nginx/" title="十分钟入门Nginx">十分钟入门Nginx</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/go/Goland%E8%BF%9C%E7%A8%8B%E5%BC%80%E5%8F%91%E9%85%8D%E7%BD%AE/" title="Goland远程开发配置">Goland远程开发配置</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E8%AF%BB%E4%B9%A6/%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90/" title="域名解析">域名解析</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/git/%E6%8F%90%E4%BA%A4%E8%BF%87%E7%A8%8B%E7%9C%81%E7%95%A5%E6%9F%90%E4%BA%9B%E6%96%87%E4%BB%B6/" title="提交过程省略某些文件">提交过程省略某些文件</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_DropdownButton%E7%A4%BA%E4%BE%8B/" title="Flutter_DropdownButton示例">Flutter_DropdownButton示例</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_ExpansionPanelList%E5%92%8CExpansionPanelList.radio%E7%A4%BA%E4%BE%8B/" title="Flutter_ExpansionPanelList和ExpansionPanelList">Flutter_ExpansionPanelList和ExpansionPanelList</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter%E5%BE%AE%E4%BF%A1%E5%88%86%E4%BA%AB%E9%93%BE%E6%8E%A5%E8%B7%B3%E5%9B%9EApp%E6%8C%87%E5%AE%9A%E9%A1%B5%E9%9D%A2/" title="Flutter微信分享链接跳回App指定页面">Flutter微信分享链接跳回App指定页面</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F/%E5%A4%96%E5%8C%85%E5%8F%AF%E8%83%BD%E7%9A%84%E9%97%AE%E9%A2%98/" title="外包可能的问题">外包可能的问题</a>
    </li>
    
</ul>
    </section>

    

    <section class="widget">
        <h3 class="widget-title"><a href='/categories/'>分类</a></h3>
<ul class="widget-list">
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (326)</a></li>
    
    <li><a href="https://luckly.work/categories/IT/">IT (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Kotlin/">Kotlin (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Mysql/">Mysql (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (1)</a></li>
    
    <li><a href="https://luckly.work/categories/Vue/">Vue (6)</a></li>
    
    <li><a href="https://luckly.work/categories/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/android/">android (6)</a></li>
    
    <li><a href="https://luckly.work/categories/dart/">dart (96)</a></li>
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (28)</a></li>
    
    <li><a href="https://luckly.work/categories/gin/">gin (25)</a></li>
    
    <li><a href="https://luckly.work/categories/git/">git (4)</a></li>
    
    <li><a href="https://luckly.work/categories/Go/">Go (102)</a></li>
    
    <li><a href="https://luckly.work/categories/gorm/">gorm (4)</a></li>
    
    <li><a href="https://luckly.work/categories/grpc/">grpc (1)</a></li>
    
    <li><a href="https://luckly.work/categories/html/">html (3)</a></li>
    
    <li><a href="https://luckly.work/categories/ios/">ios (1)</a></li>
    
    <li><a href="https://luckly.work/categories/linux/">linux (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (6)</a></li>
    
    <li><a href="https://luckly.work/categories/python/">python (35)</a></li>
    
    <li><a href="https://luckly.work/categories/read/">读书笔记 (6)</a></li>
    
    <li><a href="https://luckly.work/categories/redis/">redis (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E5%8D%95/">书单 (8)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E8%AF%84/">书评 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师 (19)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师 (25)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E5%88%99/">原则 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%9F%BA%E7%A1%80/">基础 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%BF%83%E8%AF%AD/">心语 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%8F%92%E4%BB%B6/">插件 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%95%99%E5%AD%A6/">教学 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%B2%BE%E8%BF%9B/">精进 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%BC%96%E7%A8%8B/">编程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%99%9A%E5%B9%BB/">虚幻 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%91%E6%AD%A5/">跑步 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%BA%BF/">路线 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%A1%B9%E7%9B%AE/">项目 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%AD%85%E5%8A%9B/">魅力 (1)</a></li>
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title"><a href='/tags/'>标签</a></h3>
<div class="tagcloud">
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/IT/">IT</a>
    
    <a href="https://luckly.work/tags/Kotlin/">Kotlin</a>
    
    <a href="https://luckly.work/tags/Mysql/">Mysql</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/Vue/">Vue</a>
    
    <a href="https://luckly.work/tags/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载</a>
    
    <a href="https://luckly.work/tags/android/">android</a>
    
    <a href="https://luckly.work/tags/dart/">dart</a>
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/gin/">gin</a>
    
    <a href="https://luckly.work/tags/git/">git</a>
    
    <a href="https://luckly.work/tags/go/">go</a>
    
    <a href="https://luckly.work/tags/gorm/">gorm</a>
    
    <a href="https://luckly.work/tags/grpc/">grpc</a>
    
    <a href="https://luckly.work/tags/html/">html</a>
    
    <a href="https://luckly.work/tags/ios/">ios</a>
    
    <a href="https://luckly.work/tags/linux/">linux</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/python/">python</a>
    
    <a href="https://luckly.work/tags/redis/">redis</a>
    
    <a href="https://luckly.work/tags/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E5%8D%95/">书单</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E8%AF%84/">书评</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师</a>
    
    <a href="https://luckly.work/tags/%E5%85%A5%E9%97%A8/">入门</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E5%88%99/">原则</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯</a>
    
    <a href="https://luckly.work/tags/%E5%9F%BA%E7%A1%80/">基础</a>
    
    <a href="https://luckly.work/tags/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应</a>
    
    <a href="https://luckly.work/tags/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程</a>
    
    <a href="https://luckly.work/tags/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路</a>
    
    <a href="https://luckly.work/tags/%E5%BF%83%E8%AF%AD/">心语</a>
    
    <a href="https://luckly.work/tags/%E6%8F%92%E4%BB%B6/">插件</a>
    
    <a href="https://luckly.work/tags/%E6%95%99%E5%AD%A6/">教学</a>
    
    <a href="https://luckly.work/tags/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明</a>
    
    <a href="https://luckly.work/tags/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书</a>
    
    <a href="https://luckly.work/tags/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单</a>
    
    <a href="https://luckly.work/tags/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫</a>
    
    <a href="https://luckly.work/tags/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸</a>
    
    <a href="https://luckly.work/tags/%E7%B2%BE%E8%BF%9B/">精进</a>
    
    <a href="https://luckly.work/tags/%E7%BC%96%E7%A8%8B/">编程</a>
    
    <a href="https://luckly.work/tags/%E8%99%9A%E5%B9%BB/">虚幻</a>
    
    <a href="https://luckly.work/tags/%E8%AF%97/">诗</a>
    
    <a href="https://luckly.work/tags/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/">读书笔记</a>
    
    <a href="https://luckly.work/tags/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路</a>
    
    <a href="https://luckly.work/tags/%E8%B7%91%E6%AD%A5/">跑步</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%BA%BF/">路线</a>
    
    <a href="https://luckly.work/tags/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施</a>
    
    <a href="https://luckly.work/tags/%E9%80%9A%E8%AE%AF%E5%BD%95/">通讯录</a>
    
    <a href="https://luckly.work/tags/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学</a>
    
    <a href="https://luckly.work/tags/%E9%A1%B9%E7%9B%AE/">项目</a>
    
    <a href="https://luckly.work/tags/%E9%AD%85%E5%8A%9B/">魅力</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="http://www.topgoer.com/" title="枯藤">枯藤</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gorm.cn/zh_CN/docs/index.html" title="gorm">gorm</a>
        </li>
        
        <li>
            <a target="_blank" href="https://docs.python.org/zh-cn/3/tutorial/index.html" title="python">python</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.liwenzhou.com/" title="李文周">李文周的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://www.xbzweb.com/" title="小包子的博客">小包子的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.flysnow.org/" title="飞雪无情的博客">飞雪无情的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://sliverhorn.com/" title="sliverhorn的博客">sliverhorn的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://yuedu.baidu.com/ebook/14a722970740be1e640e9a3e" title="Android Gradle权威指南">Android Gradle权威指南</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gesdh.cn/" title="小格子">格子导航</a>
        </li>
        
        <li>
            <a target="_blank" href="https://itachi.xyz/" title="阿林">itachi&#39;s Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://darjun.github.io/" title="大俊">大俊Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://geektutu.com/post/quick-golang.html" title="极客兔兔">极客兔兔Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="http://zxfcumtcs.github.io/" title="赵雪峰">雪峰Blog</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="https://luckly.work/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>
</body>

</html>